//
//  StartViewController.m
//  ImageAnimation
//
//  Created by apple on 8/26/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "StartViewController.h"
//#import "ImageAnimationViewController.h"
#import "PuzzleViewController.h"
#import "FlashStarView.h"
#import "GameAudio.h"

static int imageNumber = 0;
@implementation StartViewController{
    
    GameAudio *gameAudio;
}
@synthesize panGesture=_panGesture;
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)changeImage
{
    NSArray *imagesInApp = [[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:nil];
    int index = arc4random()%imagesInApp.count;
    NSString *path = [imagesInApp objectAtIndex:index];
    myImageView.image = [UIImage imageWithContentsOfFile:path];
}

- (void)viewDidLoad
{

    [button1 setTitle:@"从媒体库中选取图片" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(picker) forControlEvents:UIControlEventTouchUpInside];

    gameAudio =[[GameAudio alloc] init];
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    // test gesture recoganizer
    _panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureSelector:)];
    self.panGesture.delegate = self;
    [myImageView addGestureRecognizer:self.panGesture];
    
    [self changeImage];
}
-(void)picker
{
    imagepicker = [[UIImagePickerController alloc] init];
    
    imagepicker.delegate = self;
    imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    imagepicker.allowsEditing = YES;
    imagepicker.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    popoverController = [[UIPopoverController alloc] initWithContentViewController:imagepicker];
    //[popoverController setPopoverContentSize:CGSizeMake(512, 374)];
    popoverController.delegate = self;
    [popoverController presentPopoverFromRect:button1.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:NO];
    
}

- (IBAction)play:(id)sender {
    [gameAudio playBackgroundMusic];
    
    PuzzleViewController *iav = [[PuzzleViewController alloc] init];
    iav.imageFromUser = myImageView.image;
    iav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [iav setStartViewController:self];
    [self presentModalViewController:iav animated:YES];
}

- (IBAction)panGestureSelector:(id)sender {
    NSLog(@"panGestureSelector");
    
}

- (IBAction)changeImageButtonClick:(id)sender {
    [self changeImage];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    NSLog(@"gestureRecognizerShouldBegin");
    
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    
    NSLog(@"2");
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    NSLog(@"3");
}
//  初始化选取
- (void)imagePickerController:(UIImagePickerController *)picker 
        didFinishPickingImage:(UIImage *)image 
                  editingInfo:(NSDictionary *)editingInfo
{
    myImageView.image = image;
    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *name =[NSHomeDirectory() stringByAppendingPathComponent:@"image1"];
    [fm createDirectoryAtPath:name withIntermediateDirectories:YES attributes:nil error:nil];
    
    
    NSString *imageName = [NSString stringWithFormat:@"%@/%d.png",name,imageNumber];
    NSData *imageData = UIImagePNGRepresentation(image);
    [fm createFileAtPath:imageName contents:imageData attributes:nil];
    //[imageData writeToFile:imageName atomically:YES];
    if ([fm fileExistsAtPath:imageName]) {
        NSLog(@"Exist!!!!!!");
    };
    imageNumber ++;
    [picker dismissModalViewControllerAnimated:YES];
    //[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

// 完成选取
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissModalViewControllerAnimated:YES];
}




- (void)viewDidUnload
{

    button1 = nil;
    button2 = nil;
    self.panGesture = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
        return YES;
    }
    return NO;
}



@end
